package com.jumi.microservice.order.convert;

import com.jumi.microservice.common.base.dto.BaseRequest;
import com.jumi.microservice.common.base.dto.BaseResponse;
import com.jumi.microservice.dto.order.CreateOrderRequest;
import com.jumi.microservice.order.context.GoodsSkuContext;
import com.jumi.microservice.order.context.OrderCreateContext;
import com.jumi.microservice.order.context.TransHandlerContext;
import org.springframework.beans.BeanUtils;

import java.util.stream.Collectors;

/**
 * @author Dirk
 * @Date 2020-08-04 17:29
 */
public class OrderConvert implements TransConvert {

    @Override
    public TransHandlerContext convertRequest2Ctx(BaseRequest request) {
        CreateOrderRequest createOrderRequest = (CreateOrderRequest) request;
        OrderCreateContext context = new OrderCreateContext();
        BeanUtils.copyProperties(createOrderRequest, context);
        TransConvert convert = new GoodsSkuConvert();
        context.setGoodsList(createOrderRequest.getGoodsList().stream()
                .map(goodsSkuRequest -> (GoodsSkuContext)convert.convertRequest2Ctx(goodsSkuRequest))
                .collect(Collectors.toList()));
        return context;
    }

    @Override
    public BaseResponse convertCtx2Response(TransHandlerContext ctx) {
        return null;
    }
}